<template>
	<view>
		<view class="edgeInsetTop"></view>
		<view class="wanl-order">
			<view class="address cu-list menu-avatar margin-bj radius-bock">
				<view class="cu-item">
					<view class="cu-avatar round wanl-bg-orange"><text class="wlIcon-weizhi1"></text></view>
					<view class="content" v-if="addressData.address" @tap="userAddress()">
						<view>
							<text class="wanl-pip margin-right-sm">{{ addressData.name }}</text>
							<text class="wanl-gray-light text-sm">{{ addressData.mobile }}</text>
						</view>
						<view class="text-sm wanl-pip text-cut">{{ addressData.province}} {{ addressData.city }} {{ addressData.district }} {{ addressData.address }}</view>
					</view>
					<view class="content" @tap="addAddress('newadd')" v-else><view class="text-sm wanl-pip text-cut">点击此处填写收货地址</view></view>
					<view class="action"><text class="wlIcon-fanhui2 text-lg"></text></view>
				</view>
			</view>
			<view v-for="(shop, index) in orderData" :key="index" class="lists bg-white margin-bj radius-bock">
				<view class="cu-list menu-avatar">
					<view class="cu-item margin-bottom">
						<view class="cu-avatar radius-bock" :style="{ backgroundImage: 'url(' + shop.goods_img + ')' }"></view>
						<view class="content" style="margin-top: 20rpx">
							<view class="text-sm text-cut-2">{{ shop.goods_name }}</view>
						</view>
					</view>
				</view>
				<form>
					<view class="cu-form-group">
						<view class="wanl-gray-light title">快递运费</view>
						<view class="picker">{{'包邮'}} <text class="text-price margin-left-xs">{{'0'}}</text></view>
					</view>
<!--					<view class="cu-form-group align-start">-->
<!--						<view class="wanl-gray-light title">备注</view>-->
<!--						<textarea maxlength="-1" v-model="shop.remarks" placeholder="订单备注可选"></textarea>-->
<!--					</view>-->
				</form>
				<view class="text-right margin-bj text-sm">
					<text class="wanl-gray">共{{ 1 }}件，</text>
					小计：
					<text class="text-price text-orange">{{ 0 }}</text>
				</view>
			</view>
			<view class="safeAreaBottom"> </view>
			<!-- 弹出层 -->
			<view class="wanlian cu-bar tabbar solid-top foot text-df">
				<view>
					<text class="wanl-gray">共{{ 1 }}件，</text>
					合计：
					<text class="text-price text-orange">{{ 0 }}</text>
				</view>
				<button v-if="loading" class="cu-btn round lg bg-gradual-orange margin-lr-bj">
					<text class="wlIcon-jiazai wlIconfont-spin margin-right-xs"></text> 提交中...
				</button>
				<button v-else @tap="addOrder" class="cu-btn round lg bg-gradual-orange margin-lr-bj"> 提交订单 </button>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			loading: false,
			reload: false,
			token: '',
			optionData: {},
			addressData: {},
			modalName: null, // 弹出层
			couponData: {},
			couponIndex: null,
			orderData: {
			}
		};
	},
	onLoad(option) {
		this.orderData =  JSON.parse(option.good_data);
		this.zhongjiangId =  option.id;
		this.loadData();
	},
	methods: {
		async loadData(address_id) {
			// 1.1.6升级
			await uni.request({
				url: '/wanlshop/order/getOrderGoodsList',
				method: 'POST',
				data: {
					data: this.optionData,
					address_id: address_id
				},
				// 1.1.8 升级
				complete: res => {
					if(res.res.code == 1){
						this.token = res.data.token;
						this.addressData = res.data.addressData;
					}else if(res.data.code == 0 && res.res.msg == '请添加收货地址'){
						this.reload = true;
						this.addAddress('newadd');
					}
				}
			});
		},
		// 提交订单
		async addOrder() {
			let address_id = this.addressData.id;
			if(address_id === 0){
				this.$wanlshop.msg('请填写地址')
				return;
			}
			
			this.loading = true;
			await uni.request({
				url: '/wanlshop/user/create_gift_order',
				method: 'POST',
				data: {
					id:this.zhongjiangId,
					address_id:address_id
				},
				success: res => {
					if(res.res.code == '1'){
						this.loading = false;
						uni.showToast({
							title:`领取成功`,
							icon: 'none'
						});
						setTimeout(()=>{
							uni.navigateBack();
						},1200)
					}
				}
			})
		},
		//添加或修改成功之后回调
		async refreshList(data, type) {
			await uni.request({
				url: '/wanlshop/address/address',
				method: 'POST',
				data: {
					data: data,
					type: 'add'
				},
				success: res => {
					this.addressData = res.data;
					// 1.1.6升级
					if(this.reload){
						this.loadData();
					}
				}
			});
		},
		
		addAddress(type) {
			this.$wanlshop.to(`/pages/user/address/addressManage?type=${type}`);
		},
		userAddress() {
			this.$wanlshop.to('/pages/user/address/address?source=1');
		},
		// 弹出层，暂时不需要但后续版本可能需要
		showModal(name) {
			this.modalName = name;
		},
		// 关闭弹出层
		hideModal() {
			this.modalName = null;
		},
		// 数组去重并字符串
		unique(arr) {
			return Array.from(new Set(arr)).join(',');
		},
		//禁止父元素滑动
		moveHandle() {}
	}
};
</script>

<style></style>
